﻿using Infrastructure.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomenModel;
using OMS.Models;
using Model.Service;
using PagedList;

namespace OMS.Controllers
{
    public class OrderingController : Controller
    {
        public IOrderingService _service = new OrderingService();

       /* public OrderingController(IOrderingService _service)
        {
            this._service = _service;
        }

        public OrderingController()
        {
            this._service = DependencyResolver.Current.GetService<IOrderingService>();
        }  */

        public ActionResult Index()
        {

            ViewBag.currentPage = 1;
            ViewBag.itemOnPage = 10;

            ViewBag.Orders = _service.GetPaginationListOfOrder(1, ViewBag.itemOnPage, null, User.Identity.Name);
            ViewBag.OrdersCount = _service.GetCountOfOrders(User.Identity.Name);

            if (ViewBag.OrdersCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage;
               
            
            return View();
            
        }

        public ActionResult OrderDetails(int orderId)
        {
            ViewBag.currentPage = 1;
            ViewBag.itemOnPage = 10;
            ViewBag.orderId = orderId;


            ViewBag.OrdersDetails = _service.GetPaginationListOfOrderDetails(1, ViewBag.itemOnPage, null, orderId);
            ViewBag.OrdersDetailsCount = _service.GetCountOfOrdersDetails(orderId);

            if (ViewBag.OrdersDetailsCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersDetailsCount / ViewBag.itemOnPage;
             
            return View();
        }

        public ActionResult CreateOrder()
        {

            return View();
        }

        public ActionResult AddItem(int? page, string currentFilter, string searchString, string sortOrder, int orderId)
        {

            ViewBag.orderId = orderId;
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DescSortParm = sortOrder == "Description" ? "Description desc" : "Description";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            int pageNumber = (page ?? 1);
            int pageSize = 10;
            var items = _service.GetItems(sortOrder, searchString);



            return View(items.ToPagedList(pageNumber, pageSize));
        }

        [HttpGet]
        public ActionResult ItemQuantity(int orderId, int itemId, string name, decimal price)
        {

            ItemQuantity model = new ItemQuantity();
            model.ItemId = itemId;
            model.OrderId = orderId;
            model.Name = name;
            model.Price = price;

            return View(model);
        }

        [HttpPost]
        public ActionResult ItemQuantity( ItemQuantity model)
        {
            _service.AddItemToOrder(model.OrderId, model.ItemId, model.Name, model.Price, model.Quantity, model.Dimension);

            return RedirectToAction("OrderDetails", "Ordering", new { orderId = model.OrderId });

        }




     /*   public ActionResult AddItemToOrder(int orderId, int itemId)
        {
            _service.AddItemToOrder(orderId, itemId);

            return RedirectToAction("OrderDetails", "Ordering", new { orderId = orderId });
        } */
         
        public ActionResult OrderTable(int? itemOnPage ,int? page )
        {
            int currentPage = page ?? 1;
            ViewBag.itemOnPage = itemOnPage ?? 10;

            ViewBag.Orders = _service.GetPaginationListOfOrder(currentPage, ViewBag.itemOnPage, null, User.Identity.Name);
            ViewBag.OrdersCount = _service.GetCountOfOrders(User.Identity.Name);

            if (ViewBag.OrdersCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersCount / ViewBag.itemOnPage;

            ViewBag.currentPage = currentPage;
            return PartialView();
        }

        public ActionResult OrderDetailsTable(int? itemOnPage, int? page, int orderId)
        {
            int currentPage = page ?? 1;
            ViewBag.itemOnPage = itemOnPage ?? 10;
            ViewBag.orderId = orderId;

            ViewBag.OrdersDetails = _service.GetPaginationListOfOrderDetails(currentPage, ViewBag.itemOnPage, null, ViewBag.orderId);
            ViewBag.OrdersDetailsCount = _service.GetCountOfOrdersDetails(orderId);

            if (ViewBag.OrdersDetailsCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.OrdersDetailsCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.OrdersDetailsCount / ViewBag.itemOnPage;

            ViewBag.currentPage = currentPage;
            return PartialView();
        }
    }
}
